package enumStudy;

import java.io.File;
import java.lang.reflect.Method;

import static com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.PACKAGE_NAME;
import static java.io.File.separatorChar;

/**
 * @author Cheng Jun
 * @version 1.0
 * @date 2019/11/19 14:11
 */
public class EnumTest {

    public static void main(String[] args) throws Exception {
        // testSeasonEnum();
        File file = new File("src/enumStudy");
        recursion(file);
    }

    public static void testSeasonEnum() {
        for (SeasonEnum value : SeasonEnum.values()) {
            System.out.println(value.name());
            System.out.println(value.getChineseName());
            System.out.println(value.getCode());
        }
    }

    public static void recursion(File file) throws Exception {
        if (file.isDirectory()) {
            for (File listFile : file.listFiles()) {
                recursion(listFile);
            }
        } else {
            // Class.forName("enumStudy.enums.TimeUnitEnum").getEnumConstants()
            // "src\\enumStudy\\enums\\TimeUnitEnum.java"
            String path = file.getPath();
            String replace = path.replace(separatorChar, '.');
            int firstDot = replace.indexOf(".");
            int lastDot = replace.lastIndexOf(".");
            String classPath = replace.substring(firstDot + 1, lastDot);

            Class<?> enumClass = Class.forName(classPath);
            if (enumClass.isEnum()) {
                Method getCode = enumClass.getMethod("getCode");
                Method getChineseName = enumClass.getMethod("getChineseName");
                //System.out.println("id:" + Integer.valueOf(getId.invoke(object).toString()) + "->describe:" + getDescribe.invoke(object).toString());
                for (Object enumConstant : enumClass.getEnumConstants()) {
                    Integer code = Integer.valueOf(getCode.invoke(enumConstant).toString());
                    String chineseName = getChineseName.invoke(enumConstant).toString();
                    System.out.println("code: " + code + "-----" + "chineseName: " + chineseName);
                }

            }
            System.out.println(file.getName());
        }
    }


}
